#################################################################################################
###  Replication Code for Analysis: "Do Primaries Improve Electoral Performance? Clientelism
###  and Intra-Party Conflict in Ghana," American Journal of Political Science
###
###  IchinoNathan_AJPS_primaries_analysis.R
###
###  Nahomi Ichino and Noah Nathan
###  Department of Government, Harvard University
###  June 2012 
###  
###  Note: 
###  This produces Figure 1 and the results in Tables 1 and 2, by calling the matching output
###  ("matchingoutput.Rdata") from IchinoNathan_AJPS_primaries_matching.R.  It is not necessary 
###  to run the matching again before running this analysis code.
###
##############
###  Calls: 
###  primariesdata_NDC.csv
###  primariesdata_NPP.csv
###  matchingoutput.Rdata
#################################################################################################

##########################################################################################
###  Figure 1: Beanplots
##########################################################################################

data1 <- read.csv("primariesdata_NPP.csv")
data2 <- read.csv("primariesdata_NDC.csv") 
 
data1.treated<-subset(data1, prim_elec>0)
data1.control<-subset(data1, prim_elec<1)
data2.treated<-subset(data2, prim_elec>0)
data2.control<-subset(data2, prim_elec<1)

#install.packages("beanplot")
library(beanplot)

## Panel (a) is barplot of share of parliamentary elections won for NDC and NPP, by prim_elec
## Panel (b) is a beanplot of NDC vote share for NDC and NPP, by prim_elec
## Panel (c) is a beanplot of NPP vote share for NDC and NPP, by prim_elec

pdf(file= "fig1_paper.pdf", width = 9, height = 6, family = "Helvetica", pointsize = 10)
par(mfrow=c(1,3))

# panel (a)
t1<-table(data1$winner_t, data1$prim_elec)
t2<-table(data2$winner_t, data2$prim_elec)
# % win for no primelec, % win for primelec, first for NPP, then for NDC
t3<-matrix(c(t2[2,1]/(t2[1,1]+t2[2,1]), t2[2,2]/(t2[2,2]+t2[1,2]), t1[2,1]/(t1[1,1]+t1[2,1]), t1[2,2]/(t1[2,2]+t1[1,2])), nrow=2, dimnames=list(c("no primary", "primary"), c("NDC", "NPP")))
barplot(t3, beside=TRUE, col = c("yellow", "orange"), ylim=c(0,1), cex.lab=1.5, ylab="(a) Share of Parliamentary Elections Won", xaxt="n", main="(a)", xlab="NDC Primary          NPP Primary")
axis(1, at=c(1.5, 2.5, 4.5, 5.5), labels=c("No", "Yes", "No", "Yes"), cex.axis=1.5)
box(which = "plot", lty = "solid", lwd=1)
legend("topleft", bty="n",c("No primary election", "Primary election"),fill = c("yellow", "orange"), cex=1.5)
# panel (b)
beanplot(data2$vote_share_t~data2$prim_elec, data1$vote_share_t_other~data1$prim_elec, names=c("No   Yes", "No   Yes"), side = "b", col = list("yellow", "orange"), beanlinewd=.5, border = c("yellow2", "darkorange"), ylab="(b) NDC Vote Share", ylim=c(0,1), xlab="NDC Primary          NPP Primary", cex.lab=1.5, main="(b)", xaxt="n")
axis(1, at=c(0.8, 1.2, 1.8, 2.2), labels=c("No", "Yes", "No", "Yes"), cex.axis=1.5)
# panel (c)
beanplot(data2$vote_share_t_other~data2$prim_elec, data1$vote_share_t~data1$prim_elec, names=c("No / Yes", "No / Yes"), side = "b", col = list("yellow", "orange"), beanlinewd=.5, border = c("yellow2", "darkorange"), ylab="(c) NPP Vote Share", ylim=c(0,1), xlab="NDC Primary          NPP Primary", cex.lab=1.5, xaxt="n", main="(c)")
axis(1, at=c(0.8, 1.2, 1.8, 2.2), labels=c("No", "Yes", "No", "Yes"), cex.axis=1.5)

dev.off()



##########################################################################################
###  Table 1: Effect of Primaries on Parliamentary Elections Results
##########################################################################################
library(Matching)
ev<-c(FALSE,FALSE,TRUE,TRUE,TRUE)
ev.ndc<-c(FALSE,FALSE,TRUE,TRUE,TRUE)

load("matchingoutput.Rdata")

#####################
## NDC only (OLS)

NDC.1 <- lm(data2$winner_t ~ data2$prim_elec+data2$pres_t_1+data2$year2008+data2$ethfrac+data2$holds1+data2$incumbent)
NDC.2 <- lm(data2$vote_share_t_other ~ data2$prim_elec+data2$pres_t_1+data2$year2008+data2$ethfrac+data2$holds1+data2$incumbent)
NDC.3 <- lm(data2$vote_share_t ~ data2$prim_elec+data2$pres_t_1+data2$year2008+data2$ethfrac+data2$holds1+data2$incumbent)

## to see coefficients and standard errors reported in the article:
round(summary(NDC.1)$coefficients[2,], digits=4)
round(summary(NDC.2)$coefficients[2,], digits=4)
round(summary(NDC.3)$coefficients[2,], digits=4)

#####################
## NDC only (ATE)

x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
mout.1<-Match(Y=data2$winner_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATE", exact= ev.ndc, Weight.matrix=genout.2.ATE)
mout.2<-Match(Y=data2$vote_share_t_other, Tr=data2$prim_elec, X=x, M=1, estimand="ATE", exact= ev.ndc, Weight.matrix=genout.2.ATE)
mout.3<-Match(Y=data2$vote_share_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATE", exact= ev.ndc, Weight.matrix=genout.2.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

####################
## NDC only (ATT)

x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
mout.1<-Match(Y=data2$winner_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATT", exact= ev.ndc, Weight.matrix=genout.2)
mout.2<-Match(Y=data2$vote_share_t_other, Tr=data2$prim_elec, X=x, M=1, estimand="ATT", exact= ev.ndc, Weight.matrix=genout.2)
mout.3<-Match(Y=data2$vote_share_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATT", exact= ev.ndc, Weight.matrix=genout.2)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

####################
## NPP only (OLS)

NPP.1 <- lm(data1$winner_t ~ data1$prim_elec+data1$pres_t_1+data1$year2008+data1$ethfrac+data1$holds1+data1$incumbent)
NPP.2 <- lm(data1$vote_share_t ~ data1$prim_elec+data1$pres_t_1+data1$year2008+data1$ethfrac+data1$holds1+data1$incumbent)
NPP.3 <- lm(data1$vote_share_t_other ~ data1$prim_elec+data1$pres_t_1+data1$year2008+data1$ethfrac+data1$holds1+data1$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(NPP.1)$coefficients[2,], digits=4)
round(summary(NPP.2)$coefficients[2,], digits=4)
round(summary(NPP.3)$coefficients[2,], digits=4)

####################
## NPP only (ATE)

x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
mout.1<-Match(Y=data1$winner_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.1.ATE)
mout.2<-Match(Y=data1$vote_share_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.1.ATE)
mout.3<-Match(Y=data1$vote_share_t_other, Tr=data1$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.1.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

####################
## NPP only (ATT)

x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
mout.1<-Match(Y=data1$winner_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.1)
mout.2<-Match(Y=data1$vote_share_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.1)
mout.3<-Match(Y=data1$vote_share_t_other, Tr=data1$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.1)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

###############################################################
## Competitive NPP only (OLS)

CNPP.1 <- lm(data.cnpp$winner_t ~ data.cnpp$prim_elec+data.cnpp$pres_t_1+data.cnpp$year2008+data.cnpp$ethfrac+data.cnpp$holds1+data.cnpp$incumbent)
CNPP.2 <- lm(data.cnpp$vote_share_t ~ data.cnpp$prim_elec+data.cnpp$pres_t_1+data.cnpp$year2008+data.cnpp$ethfrac+data.cnpp$holds1+data.cnpp$incumbent)
CNPP.3 <- lm(data.cnpp$vote_share_t_other ~ data.cnpp$prim_elec+data.cnpp$pres_t_1+data.cnpp$year2008+data.cnpp$ethfrac+data.cnpp$holds1+data.cnpp$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(CNPP.1)$coefficients[2,], digits=4)
round(summary(CNPP.2)$coefficients[2,], digits=4)
round(summary(CNPP.3)$coefficients[2,], digits=4)

###############################################################
## Competitive NPP only (ATE)

x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
mout.1<-Match(Y=data.cnpp$winner_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.cnpp.ATE)
mout.2<-Match(Y=data.cnpp$vote_share_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.cnpp.ATE)
mout.3<-Match(Y=data.cnpp$vote_share_t_other, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.cnpp.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

##############################
## Competitive NPP only (ATT)

x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
mout.1<-Match(Y=data.cnpp$winner_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.cnpp)
mout.2<-Match(Y=data.cnpp$vote_share_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.cnpp)
mout.3<-Match(Y=data.cnpp$vote_share_t_other, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.cnpp)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

###########################
### Stronghold NPP (OLS)

NNPP.1 <- lm(data.nnpp$winner_t ~ data.nnpp$prim_elec+data.nnpp$pres_t_1+data.nnpp$year2008+data.nnpp$ethfrac+data.nnpp$holds1+data.nnpp$incumbent)
NNPP.2 <- lm(data.nnpp$vote_share_t ~ data.nnpp$prim_elec+data.nnpp$pres_t_1+data.nnpp$year2008+data.nnpp$ethfrac+data.nnpp$holds1+data.nnpp$incumbent)
NNPP.3 <- lm(data.nnpp$vote_share_t_other ~ data.nnpp$prim_elec+data.nnpp$pres_t_1+data.nnpp$year2008+data.nnpp$ethfrac+data.nnpp$holds1+data.nnpp$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(NNPP.1)$coefficients[2,], digits=4)
round(summary(NNPP.2)$coefficients[2,], digits=4)
round(summary(NNPP.3)$coefficients[2,], digits=4)

###########################
### Stronghold NPP (ATE)

x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
mout.1<-Match(Y=data.nnpp$winner_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.nnpp.ATE)
mout.2<-Match(Y=data.nnpp$vote_share_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.nnpp.ATE)
mout.3<-Match(Y=data.nnpp$vote_share_t_other, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.nnpp.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)

##############################
### Stronghold NPP (ATT)

x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
mout.1<-Match(Y=data.nnpp$winner_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.nnpp)
mout.2<-Match(Y=data.nnpp$vote_share_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.nnpp)
mout.3<-Match(Y=data.nnpp$vote_share_t_other, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.nnpp)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
lm3<-lm(mout.3$mdata$Y~mout.3$mdata$Tr+mout.3$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)
round(summary(lm3)$coefficients[2,], digits=4)


##########################################################################################
###  Table 2 : Effect of Primaries on Presidential Elections Results
##########################################################################################
library(Matching)
ev<-c(FALSE,FALSE,TRUE,TRUE,TRUE)
ev.ndc<-c(FALSE,FALSE,TRUE,TRUE,TRUE)

load("matchingoutput.Rdata")

#####################
### NDC only (OLS)

NDC.1 <- lm(data2$pres_t ~ data2$prim_elec+data2$pres_t_1+data2$year2008+data2$ethfrac+data2$holds1+data2$incumbent)
NDC.2 <- lm(data2$turnout_t_pres ~ data2$prim_elec+data2$pres_t_1+data2$year2008+data2$ethfrac+data2$holds1+data2$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(NDC.1)$coefficients[2,], digits=4)
round(summary(NDC.2)$coefficients[2,], digits=4)

#####################
### NDC only (ATE)

x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
mout.1<-Match(Y=data2$pres_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATE", exact= ev.ndc, Weight.matrix=genout.2.ATE)
mout.2<-Match(Y=data2$turnout_t_pres, Tr=data2$prim_elec, X=x, M=1, estimand="ATE", exact= ev.ndc, Weight.matrix=genout.2.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

####################
### NDC only (ATT)

x<-cbind(data2$pres_t_1, data2$ethfrac, data2$year2008, data2$holds1, data2$incumbent)
mout.1<-Match(Y=data2$pres_t, Tr=data2$prim_elec, X=x, M=1, estimand="ATT", exact= ev.ndc, Weight.matrix=genout.2)
mout.2<-Match(Y=data2$turnout_t_pres, Tr=data2$prim_elec, X=x, M=1, estimand="ATT", exact= ev.ndc, Weight.matrix=genout.2)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

####################
### NPP only (OLS)

NPP.1 <- lm(data1$pres_t ~ data1$prim_elec+data1$pres_t_1+data1$year2008+data1$ethfrac+data1$holds1+data1$incumbent)
NPP.2 <- lm(data1$turnout_t_pres ~ data1$prim_elec+data1$pres_t_1+data1$year2008+data1$ethfrac+data1$holds1+data1$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(NPP.1)$coefficients[2,], digits=4)
round(summary(NPP.2)$coefficients[2,], digits=4)

####################
### NPP only (ATE)

x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
mout.1<-Match(Y=data1$pres_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.1.ATE)
mout.2<-Match(Y=data1$turnout_t_pres, Tr=data1$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.1.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

####################
### NPP only (ATT)

x<-cbind(data1$pres_t_1, data1$ethfrac, data1$year2008, data1$holds1, data1$incumbent)
mout.1<-Match(Y=data1$pres_t, Tr=data1$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.1)
mout.2<-Match(Y=data1$turnout_t_pres, Tr=data1$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.1)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)


###############################################################
### Competitive NPP only (OLS)

CNPP.1 <- lm(data.cnpp$pres_t ~ data.cnpp$prim_elec+data.cnpp$pres_t_1+data.cnpp$year2008+data.cnpp$ethfrac+data.cnpp$holds1+data.cnpp$incumbent)
CNPP.2 <- lm(data.cnpp$turnout_t_pres ~ data.cnpp$prim_elec+data.cnpp$pres_t_1+data.cnpp$year2008+data.cnpp$ethfrac+data.cnpp$holds1+data.cnpp$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(CNPP.1)$coefficients[2,], digits=4)
round(summary(CNPP.2)$coefficients[2,], digits=4)

###############################################################
### Competitive NPP only (ATE)

x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
mout.1<-Match(Y=data.cnpp$pres_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.cnpp.ATE)
mout.2<-Match(Y=data.cnpp$turnout_t_pres, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.cnpp.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

##############################
### Competitive NPP only (ATT)

x<-cbind(data.cnpp$pres_t_1, data.cnpp$ethfrac, data.cnpp$year2008, data.cnpp$holds1, data.cnpp$incumbent)
mout.1<-Match(Y=data.cnpp$pres_t, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.cnpp)
mout.2<-Match(Y=data.cnpp$turnout_t_pres, Tr=data.cnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.cnpp)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

###########################
### Stronghold NPP (OLS)

NNPP.1 <- lm(data.nnpp$pres_t ~ data.nnpp$prim_elec+data.nnpp$pres_t_1+data.nnpp$year2008+data.nnpp$ethfrac+data.nnpp$holds1+data.nnpp$incumbent)
NNPP.2 <- lm(data.nnpp$turnout_t_pres ~ data.nnpp$prim_elec+data.nnpp$pres_t_1+data.nnpp$year2008+data.nnpp$ethfrac+data.nnpp$holds1+data.nnpp$incumbent)
## to see coefficients and standard errors reported in the article:
round(summary(NNPP.1)$coefficients[2,], digits=4)
round(summary(NNPP.2)$coefficients[2,], digits=4)

###########################
### Stronghold NPP (ATE)

x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
mout.1<-Match(Y=data.nnpp$pres_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.nnpp.ATE)
mout.2<-Match(Y=data.nnpp$turnout_t_pres, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATE", exact=ev, Weight.matrix=genout.nnpp.ATE)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)

##############################
### Stronghold NPP (ATT)

x<-cbind(data.nnpp$pres_t_1, data.nnpp$ethfrac, data.nnpp$year2008, data.nnpp$holds1, data.nnpp$incumbent)
mout.1<-Match(Y=data.nnpp$pres_t, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.nnpp)
mout.2<-Match(Y=data.nnpp$turnout_t_pres, Tr=data.nnpp$prim_elec, X=x, M=1, estimand="ATT", exact=ev, Weight.matrix=genout.nnpp)
## matching estimates with regression adjustment
lm1<-lm(mout.1$mdata$Y~mout.1$mdata$Tr+mout.1$mdata$X)
lm2<-lm(mout.2$mdata$Y~mout.2$mdata$Tr+mout.2$mdata$X)
## to see estimates and standard errors reported in the article:
round(summary(lm1)$coefficients[2,], digits=4)
round(summary(lm2)$coefficients[2,], digits=4)


















